package com.yihu.cms.action;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.yihu.beans.ResultVO;
import com.yihu.beans.user.Group;
import com.yihu.beans.user.GroupQuery;
import com.yihu.beans.user.Role;
import com.yihu.beans.user.User;
import com.yihu.beans.user.UserGroupRef;
import com.yihu.cms.service.UserGroupService;
import com.yihu.cms.service.WexinMsgByGroupService;

@Controller
public class UserGroupController extends BaseController{
	

	private UserGroupService us = new UserGroupService();
	@RequestMapping(value = "/user/group" , method = RequestMethod.POST)
	public @ResponseBody Object addGroup(@RequestBody Group group) {
		log.debug("addGroup: "+group.toString());
		ResultVO result= us.addGroup(group);
		log.debug(JSON.toJSONString(result));
		return JSON.toJSONString(result);
	}
	
	@RequestMapping(value = "/user/group/{id}", method = RequestMethod.PUT)
	public @ResponseBody Object updateGroup(@RequestBody Group group,@PathVariable("id") long id) {
		group.setGroupId(id);
		log.debug("updateGroup: "+group.toString());
		ResultVO result= us.updateGroup(group);
		return JSON.toJSONString(result);
	}
	
	@RequestMapping(value = "/user/group/{id}", method = RequestMethod.DELETE)
	public @ResponseBody Object deleteGroup(@PathVariable("id") long id) {
		log.debug("deleteGroup: "+id);
		ResultVO result= us.deleteGroup(id);
		return JSON.toJSONString(result);
	}
	
	@RequestMapping(value = "/user/group/{id}", method = RequestMethod.GET)
	public @ResponseBody Object getGroup(@PathVariable("id") long id) {
		log.debug("getGroup's id: "+id);
		Group group = us.getGroup(id);
		return JSON.parseObject(JSON.toJSONString(group), Group.class);
	}
	
	@RequestMapping(value = "/user/group/list" , method = RequestMethod.GET)
	// , method = RequestMethod.PATCH
	public @ResponseBody Object getGroupsPaging(GroupQuery query) {
		log.debug("getUsersPaging "+query.toString());
		String resultJson= us.getGroupByPage(query);
		return JSON.parse(resultJson);
	}
	
	@RequestMapping(value = "/user/group/users", method = RequestMethod.POST)
	public @ResponseBody Object getGroupUsers(@RequestBody GroupQuery query) {

		log.debug("getGroupUsers : "+query);
		Group group = us.getUsersByGroupId(query);
		return JSON.parse(JSON.toJSONString(group));
	}
	
	/***
	 * 
	 * @param query groupName,desc
	 * @return
	 */
	@RequestMapping(value = "/user/group/users", method = RequestMethod.GET)
	public @ResponseBody Object getUsersByGroup(GroupQuery query) {

		log.debug("getGroupUsers : "+query);
		List<User> users = us.getUsersByGroup(query);
		return users;
	}
	
	@RequestMapping(value = "/user/group/addUser", method = RequestMethod.POST)
	public @ResponseBody Object addUserToGroup(@RequestBody UserGroupRef userGroupRef) {
		log.debug("addUserToGroup : "+userGroupRef);
		ResultVO r = us.addUserToGroup(userGroupRef);
		return JSON.toJSONString(r);
	}
	
	@RequestMapping(value = "/user/group/deleteUser", method = RequestMethod.POST)
	public @ResponseBody Object deleteUserFromGroup(@RequestBody UserGroupRef userGroupRef) {
		log.debug("addUserToGroup : "+userGroupRef);
		ResultVO r = us.deleteUserFromGroup(userGroupRef);
		return JSON.toJSONString(r);
	}
	
	@RequestMapping(value = "/user/group/assignUsers", method = RequestMethod.POST)
	public @ResponseBody Object assignUsers(@RequestBody UserGroupRef userGroupRef) {
		log.debug("assignUsers : "+userGroupRef);
		ResultVO r = us.assignUsers(userGroupRef);
		return JSON.toJSONString(r);
	}
	
	/***
	 * 获取咨询组的组员openid
	 * @param query
	 * @return
	 */
	@RequestMapping(value = "/user/group/consult", method = RequestMethod.GET)
	public @ResponseBody Object getGroupConsult() {
		log.debug("getGroupConsult ");
		List<String> openids = WexinMsgByGroupService.getConsultService();
		return openids;
	}
}
